package com.asa.ui.F;

import java.awt.EventQueue;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.SwingUtilities;

import com.asa.ui.F.AlgoFrame;
import com.asa.ui.F.AlgoVisualizer.AlgoMouseListener;
import com.asa.ui.frame.AlgoVisHelper;

public class AlgoVisualizer {
	private AlgoFrame frame;
	private MineSweeperData data;
	private int canvasWidth;
	private int canvasHeight;
	private int mineNumber;

	public AlgoVisualizer(int M, int N, int mineNumber) {
		data = new MineSweeperData(M, N, mineNumber);
		canvasWidth = ImageUrlUtil.BLOCK_WIDTH * data.getN();
		canvasHeight = ImageUrlUtil.BLOCK_WIDTH * data.getM();
		this.mineNumber = mineNumber;
//	  EventQueue.invokeLater(()->{
//		  frame=new AlgoFrame("扫雷", canvasWidth, canvasHeight);
//		  frame.addMouseListener(new AlgoMouseListener());
//		  new Thread(()->{
//			  run();
//		  }).start();
//	  });
		
		
		EventQueue.invokeLater(new Runnable() {//用java事件分发（开了个线程）的方法来创建对话框
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				frame=new AlgoFrame("扫雷", canvasWidth, canvasHeight);	
				frame.addMouseListener(new AlgoMouseListener());
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						runmiage();
					}

					
				}).start(); ;
				
			}
		});
		
		
		
		

	}

	private void runmiage() {
		setData(false, -1, -1);
	}

	private void setData(boolean isopen, int x, int y) {
		if (data.isArea(x, y)) {
			if (isopen) {
				data.open[x][y] = true;
			} else {
				data.flag[x][y] = true;
			}
		}
		frame.render(data);
		AlgoVisHelper.pause(20);
	}

	class AlgoMouseListener extends MouseAdapter {
		public void mouseReleased(MouseEvent e) {
			e.translatePoint(-(int) (frame.getBounds().width - canvasWidth),
					-(int) (frame.getBounds().height - canvasHeight));
			Point pos = e.getPoint();
			int x = pos.y / ImageUrlUtil.BLOCK_WIDTH;
			int y = pos.x / ImageUrlUtil.BLOCK_WIDTH;
			if (SwingUtilities.isRightMouseButton(e)) {
				setData(false, x, y);
			} else if (SwingUtilities.isLeftMouseButton(e)) {
				setData(true, x, y);
				if (!data.mine[x][y]) {
					open(x, y);
				} else {
					try {
						bang();
						frame.showInfo("游戏失败");
					} catch (Exception l) {

					} finally {
						data = new MineSweeperData(data.getM(), data.getN(), mineNumber);
						setData(false, -1, -1);
					}
				}
			}

		}

		private void open(int x, int y) {
			if (data.number[x][y] > 0) {
				return;
			}
			data.open[x][y] = true;
			for (int i = x - 1; i <= x + 1; i++) {
				for (int j = y - 1; j <= y + 1; j++) {
					if (data.isArea(i, j) && !data.open[i][j] && !data.isMine(i, j)) {
						open(i, j);
					}
				}
			}
		}
	}

	public void bang() {
		for (int i = 0; i < data.getM(); i++) {
			for (int j = 0; j < data.getN(); j++) {
				if (data.isMine(i, j)) {
					setData(true, i, j);
				}
			}
		}
	}

	
}
